﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Linq;
using System.Text;
using System.Drawing;
using Bamboo.Core.Common.Reflection;
using System.Windows.Forms;
using System.Xml;
using System.IO;
using TinyERP.Framework.Controls;
using Crownwood.Magic.Common;
using System.Globalization;
using System.Threading;

namespace TinyERP.Framework
{
    public class UIManager
    {
        private static UIManager instance;
        private INavigator navigator;
        private IContentPane contentPanel;
        public INavigator Navigator
        {
            get
            {
                return navigator;
            }
        }
        public IContentPane ContentPanel
        {
            get
            {
                return contentPanel;
            }
        }

        public void LoadGlobalShortcuts()
        {
            StandardShortcutKey.CLOSE = ApplicationConfiguration.GetInstance().CloseShortcut;
            StandardShortcutKey.DELETE = ApplicationConfiguration.GetInstance().DeleteShortcut;
            StandardShortcutKey.FIND = ApplicationConfiguration.GetInstance().FindShortcut;
            StandardShortcutKey.EDIT = ApplicationConfiguration.GetInstance().EditShortcut;
            StandardShortcutKey.NEW = ApplicationConfiguration.GetInstance().NewShortcut;
        }

        private UIManager()
        {

        }

        public static UIManager GetInstance()
        {
            if (instance == null)
            {
                instance = new UIManager();
            }
            return instance;
        }

        public Control CreateNavigator()
        {
            if (ApplicationConfiguration.GetInstance().NavigationView == "OutlookBar")
            {
                this.navigator = new OutlookNavigator();
            }
            else
            {
                this.navigator = new TreeNavigator();
            }
            this.LoadApplicationStructure();
            this.navigator.OnSelectedNode += new SelectNodeHandler(OnNavigatorSelectedNode);
            return this.Navigator as Control;
        }
        public Control CreateContentPane()
        {
            TabContentPane tabControl = new TabContentPane();
            tabControl.Dock = DockStyle.Fill;
            tabControl.Appearance = Crownwood.Magic.Controls.TabControl.VisualAppearance.MultiDocument;

            tabControl.Style = VisualStyle.IDE;
            tabControl.IDEPixelBorder = true;
            tabControl.AllowDrop = true;
            this.contentPanel = tabControl;
            return tabControl;

        }
        private void OnNavigatorSelectedNode(object sender, ApplicationNode selectedNode)
        {
            if (!string.IsNullOrEmpty(selectedNode.UIClassName))
            {
                contentPanel.ShowPane(selectedNode);
            }
        }

        private void LoadApplicationStructure()
        {
            IList<ApplicationNode> nodeList = ApplicationConfiguration.GetInstance().ApplicationStructure;
            foreach (ApplicationNode node in nodeList)
            {
                navigator.AddNode(node);
            }
        }
        public void ChangeLanguage(string language, Form form)
        {
            CultureInfo cultureInfo = new CultureInfo(language);
            
            Thread.CurrentThread.CurrentUICulture = cultureInfo;
            foreach (Control control in form.Controls)
            {
                ChangeLanguageOfControl(control);
            }
        }
        private void ChangeLanguageOfControl(Control control)
        {
            if (control is ILocalizable)
            {
                ((ILocalizable)control).UpdateLanguage();
            }
            if (control.Controls != null && control.Controls.Count > 0)
            {
                foreach (Control childControl in control.Controls)
                {
                    ChangeLanguageOfControl(childControl);
                }
            }
        }
    }
}
